package com.example.base.activity;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.base.R;
import com.example.base.adapter.SystemTwoViewpager;
import com.example.base.bean.SystemOne;
import com.example.base.fragment.SystemTwoFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SystemActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private final List<Fragment> fragments = new ArrayList<>();
    private final List<String> titles = new ArrayList<>();
    private String title;
    private int number;
    private SystemTwoViewpager systemTwoViewpager;
    private SystemOne systemOne;
    private TextView textView;
    private ImageButton imageButton;
    private final String TAG = "SystemActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system);
        initView();
        initListener();
        getIntentData();
        showActionBar();
        getDataFromNet();
    }

    private void getDataFromNet() {
        OkHttpClient mOkHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url("https://www.wanandroid.com/tree/json")
                .get()
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                if (response.isSuccessful()) {
                    Gson gson = new Gson();
                    if (response.body() == null) {
                        Log.d(TAG, "response.body() isNull");
                        return;
                    }
                    systemOne = gson.fromJson(response.body().string(), SystemOne.class);
                    runOnUiThread(() -> {
                        for (int i = 0; i < systemOne.getData().get(number).getChildren().size(); i++) {
                            titles.add(systemOne.getData().get(number).getChildren().get(i).getName());
                            tabLayout.addTab(tabLayout.newTab());
                            if (null != titles.get(i)){
                                Objects.requireNonNull(tabLayout.getTabAt(i)).setText(titles.get(i));
                            }

                            SystemTwoFragment myFragment = new SystemTwoFragment();
                            Bundle bundle = new Bundle();
                            bundle.putInt("ID", systemOne.getData().get(number).getChildren().get(i).getId());//这里的values就是我们要传的值

                            myFragment.setArguments(bundle);
                            fragments.add(myFragment);
                        }
                        Log.i("System", titles + "");
                        systemTwoViewpager.notifyDataSetChanged();
                    });
                }
            }
        });
        systemTwoViewpager = new SystemTwoViewpager(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(systemTwoViewpager);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabsFromPagerAdapter(systemTwoViewpager);
    }

    private void showActionBar() {
        ActionBar bar = getSupportActionBar();
        if (null != bar){
            bar.hide();
        }
        textView.setText(title);
    }

    private void getIntentData() {
        Intent intent = getIntent();
        title = intent.getStringExtra("title");
        number = intent.getIntExtra("number", number);
    }

    private void initListener() {
        imageButton.setOnClickListener(v -> finish());
    }

    private void initView() {
        textView = findViewById(R.id.textView_systwo);
        tabLayout = findViewById(R.id.tablayout_systemtwo);
        viewPager = findViewById(R.id.viewpaper_systemtwo);
        imageButton = findViewById(R.id.img_system_back);
    }


}